package com.qinglei.recoup.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qinglei.recoup.system.domain.Dict;
import com.qinglei.recoup.system.dao.DictMapper;
import com.qinglei.recoup.system.domain.DictBO;
import com.qinglei.recoup.system.service.DictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author syp
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Override
    public DictBO getDictItem(String itemGroup, Long key) {
        Dict dict = baseMapper.selectOne(Wrappers.<Dict>lambdaQuery().eq(Dict::getItemGroup, itemGroup).eq(Dict::getKeyy, key));
        if (dict != null) {
            DictBO dictBO = new DictBO();
            BeanUtils.copyProperties(dict, dictBO);
            return dictBO;
        }
        return null;
    }

    @Override
    public List<DictBO> getDictItemList(String itemGroup) {
        List<Dict> listDict = baseMapper.selectList(Wrappers.<Dict>lambdaQuery().eq(Dict::getItemGroup,
                itemGroup).orderByAsc(Dict::getItemType).orderByAsc(Dict::getKeyy));
        if (CollectionUtils.isEmpty(listDict)) {
            return Collections.emptyList();
        }
        List<DictBO> dictBOList = listDict.stream().map(item ->{
            DictBO dictBO = new DictBO();
            BeanUtils.copyProperties(item, dictBO);
            return dictBO;
        }).collect(Collectors.toList());
        return dictBOList;
    }
}
